﻿namespace zyn_hy_login
{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;

    public class CheckName
    {
        protected static string FirstConn = ConfigurationSettings.AppSettings["connectString"];
        private string stringConn;
        private string stringTable;
        private string tempID2;
        private string tempID3;
        private string tempName;

        public CheckName(string ID2, string ID3, string Name)
        {
            this.tempName = Name;
            this.tempID2 = ID2;
            this.tempID3 = ID3;
            this.First();
        }

        public void First()
        {
            SqlCommand command = new SqlConnection(FirstConn).CreateCommand();
            command.CommandType = CommandType.Text;
            command.CommandText = "select x1 server,x2 Data,x3 uid,x4 pwd,x5 biao from d_a where ID=" + this.tempID3 + "";
            command.Connection.Open();
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            try
            {
                while (reader.Read())
                {
                    this.stringConn = "server=" + reader.GetValue(0).ToString() + ";User ID=" + reader.GetValue(2).ToString() + ";Password=" + reader.GetValue(3).ToString() + ";database=" + reader.GetValue(1).ToString() + ";Connection Reset=FALSE";
                    this.stringTable = reader.GetValue(4).ToString();
                }
            }
            finally
            {
                reader.Close();
            }
        }

        public bool ValidateName()
        {
            this.First();
            SqlConnection connection = new SqlConnection(this.stringConn);
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.Text;
            command.CommandText = "select name,id2 from " + this.stringTable.ToString() + " where name='" + this.tempName + "' and  ID2=" + this.tempID2.ToString() + "";
            command.Connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            try
            {
                if (reader.Read())
                {
                    return false;
                }
            }
            finally
            {
                reader.Close();
                connection.Close();
            }
            return true;
        }

        public string RecordConn
        {
            get
            {
                return this.stringConn;
            }
        }

        public string TableName
        {
            get
            {
                return this.stringTable;
            }
        }
    }
}

